package resilienceManager.qmul.gemom;

import java.io.File;
import java.io.FileInputStream;
import java.lang.*;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;

import com.sun.org.apache.xerces.internal.parsers.*; 



/**
 * A parser for the scenario 
 * import the scenario configuration from xml file 
 * 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */

public class scParser {

	// the number of publisher
	private int pu_num=0;

	// the number of gbroker
	private int gb_num=0;


	// the vector of plist
	private Vector<Publisher> plist;

	// the vector of gbroker
	private Vector<Gbroker> glist;

	// the vector of path event
	private Vector<Integer> pathlist;

	// the vector of gbroker event
	private Vector<Integer> bglist;


	private TreeBuilder saxTree = null;

	private static String file = "";

	private String[] tag={"publisher","Gbroker","path","broken"};
	
	
	Vector<DefaultMutableTreeNode> list;


	public scParser(File xmlfile)
	{
		DefaultMutableTreeNode top = new DefaultMutableTreeNode("root");
		saxTree = new TreeBuilder(top);

		if(xmlfile.exists())
		{
			file=xmlfile.getAbsolutePath();
		}
		else{
			JOptionPane.showMessageDialog(null, "Wrong File or File damage!"); 
			return; 
		}


		try{
			SAXParser saxParser = new SAXParser();
			saxParser.setContentHandler(saxTree);
			saxParser.parse(new InputSource(new FileInputStream(file)));
		}catch(Exception ex){
			ex.printStackTrace();
		}

		plist=new Vector<Publisher>();

		glist=new Vector<Gbroker>();

		pathlist=new Vector<Integer>();

		bglist=new Vector<Integer>();
		
		list=new Vector<DefaultMutableTreeNode>();

		analysis();

	}

	private void analysis()
	{
		DefaultMutableTreeNode root=saxTree.getTree();
		
		list.removeAllElements();
		
		findNode(tag[0],root);

		for(int i=0; i<list.size(); i++)
		{
			
			Publisher tp;

			int id=Integer.parseInt(list.get(i).getFirstLeaf().toString());

			double s=Double.parseDouble(list.get(i).getLastLeaf().toString());

			tp=new Publisher(id,s);

			plist.add(tp);
		}
		
		list.removeAllElements();
		
		findNode(tag[1],root);


		for(int i=0; i<list.size(); i++)
		{
			Gbroker tp;
			
			DefaultMutableTreeNode nodeID = list.get(i).getFirstLeaf();
			int id=Integer.parseInt(nodeID.toString());
			DefaultMutableTreeNode nodeCap = nodeID.getNextLeaf();
			double c=Double.parseDouble(nodeCap.toString());
			DefaultMutableTreeNode nodeIP = nodeCap.getNextLeaf();
			String s=nodeIP.toString();
			tp=new Gbroker(id,c, s);
			glist.add(tp);
		}
		
		list.removeAllElements();
		
		findNode(tag[2],root);

		for(int i=0; i<list.size(); i++)
		{	
			int path=Integer.parseInt(list.get(i).getFirstLeaf().toString());

			pathlist.add(path);
		}
		
		list.removeAllElements();
		
		findNode(tag[3],root);


		for(int i=0; i<list.size(); i++)
		{	
			int bg=Integer.parseInt(list.get(i).getFirstLeaf().toString());

			bglist.add(bg);
		}

		list.removeAllElements();
		
	}
	
	/**
	 * 
	 * @param String, the tag  you want to find
	 * @param DefaultMutableTreeNode, the node you want to find
	 * */

	private void findNode(String s, DefaultMutableTreeNode node)
	{
		
		if (node.getChildCount() >= 0) {
			for (Enumeration e=node.children(); e.hasMoreElements(); ) {
				DefaultMutableTreeNode n = (DefaultMutableTreeNode)e.nextElement();
				if(n.toString().equals(s))
					list.add(n);
				else
					findNode(s,n);
			}
		}
	}




	public int getNum(String s)
	{
		if(s.equals("publiser"))
			return this.plist.size();

		if(s.equals("gbroker"))
			return this.glist.size();

		return -1;
	}

	public Vector<Gbroker> getGB()
	{
		return glist;
	}

	public Vector<Publisher> getPU()
	{
		return plist;
	}
	
	public Vector<Integer> getBP()
	{
		return pathlist; 
	}
	
	public Vector<Integer> getBN()
	{
		return bglist;
	}

}
